unit Dictionary;

{
Release: 2006-05-09

Copyright (c) 2006, Lars Kiesow
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
}

interface

uses Classes;

type
  TDictiorary = class
    protected
      FS: array [0..1] of TStringList;
      FDelimeter: Char;
      function GetS1(AIndex: Integer): String;
      function GetS2(AIndex: Integer): String;
      procedure SetS1(AIndex: Integer; AString: String);
      procedure SetS2(AIndex: Integer; AString: String);
      function GetItems(AIndex: Integer): String;
      procedure SetItems(AIndex: Integer; AString: String);
      procedure Sort(const ALng: Byte; const Aufsteigend: Boolean);
    public
      constructor Create;
      destructor Destroy; override;

      procedure Add(ALng1, ALng2: String);
      procedure Delete(AIndex: Integer);
      procedure Change(AIndex: Integer; ALng1, ALng2: String);
      procedure Clear;
      procedure Shuffle;
      procedure Sort_S1(const Aufsteigend: Boolean);
      procedure Sort_S2(const Aufsteigend: Boolean);

      function Count: Integer;

      property Lng1[Index: Integer]: String read GetS1 write SetS1;
      property Lng2[Index: Integer]: String read GetS2 write SetS2;
      property Items[Index: Integer]: String read GetItems write SetItems; default;
      property Delimeter: Char read FDelimeter write FDelimeter;
  end;

implementation

uses Math;

constructor TDictiorary.Create;
begin
  inherited Create;
  FS[0] := TStringList.Create;
  FS[1] := TStringList.Create;
  FDelimeter := ';';
end;

destructor TDictiorary.Destroy;
begin
  FS[0].Free;
  FS[1].Free;
  inherited Destroy;
end;

procedure TDictiorary.Add(ALng1, ALng2: String);
begin
  FS[0].Add(ALng1);
  FS[1].Add(ALng2);
end;

procedure TDictiorary.Delete(AIndex: Integer);
begin
  FS[0].Delete(AIndex);
  FS[1].Delete(AIndex);
end;

function TDictiorary.GetS1(AIndex: Integer): String;
begin
  Result := FS[0][AIndex];
end;

function TDictiorary.GetS2(AIndex: Integer): String;
begin
  Result := FS[1][AIndex];
end;

procedure TDictiorary.SetS1(AIndex: Integer; AString: String);
begin
  FS[0][AIndex] := AString;
end;

procedure TDictiorary.SetS2(AIndex: Integer; AString: String);
begin
  FS[1][AIndex] := AString;
end;

function TDictiorary.GetItems(AIndex: Integer): String;
begin
  Result := Lng1[AIndex] + FDelimeter + Lng2[AIndex];
end;

procedure TDictiorary.SetItems(AIndex: Integer; AString: String);
var
  i: Integer;
begin
  i := Pos(FDelimeter, AString);
  FS[0][AIndex] := Copy(AString, 1, Pred(i));
  FS[1][AIndex] := Copy(AString, Succ(i), Length(AString)-i);
end;

procedure TDictiorary.Change(AIndex: Integer; ALng1, ALng2: String);
begin
  FS[0][AIndex] := ALng1;
  FS[1][AIndex] := ALng2;
end;

procedure TDictiorary.Shuffle;
var
  i, j: integer;
begin
  for i := Pred(FS[0].Count) downto 0 do
  begin
    j := Random(i);
    FS[0].Move(j, Pred(FS[0].Count));
    FS[1].Move(j, Pred(FS[1].Count));
  end;
end;

procedure TDictiorary.Sort(const ALng: Byte; const Aufsteigend: Boolean);
var
  i: integer;
  s: string;
begin
  i := 1;
  s := FS[ALng][0];
  while i < FS[ALng].Count do
  begin
    s := FS[ALng][i];
    if ((FS[ALng][Pred(i)] > FS[ALng][i]) = Aufsteigend) and
       (FS[ALng][Pred(i)] <> FS[ALng][i]) then
    begin
      FS[0].Exchange(Pred(i), i);
      FS[1].Exchange(Pred(i), i);
      i := 1;
      s := FS[ALng][0];
    end else
      inc(i);
  end;
end;

procedure TDictiorary.Sort_S1(const Aufsteigend: Boolean);
begin
  Sort(0, Aufsteigend);
end;

procedure TDictiorary.Sort_S2(const Aufsteigend: Boolean);
begin
  Sort(1, Aufsteigend);
end;

function TDictiorary.Count: Integer;
begin
  Result := FS[0].Count;
end;

procedure TDictiorary.Clear;
begin
  FS[0].Clear;
  FS[1].Clear;
end;

end.